local sk__qianchong = fk.CreateSkill {

  name = "sk__qianchong",

  tags = {  },

}



sk__qianchong:addEffect(fk.TurnEnd, {
  name = "sk__qianchong",
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__qianchong.name) then
      local choices = {}
      if #player.room.logic:getEventsOfScope(GameEvent.UseCard, 2, function(e)
        local use = e.data
        return use.from == player and use.card.color == Card.Red
      end, Player.HistoryTurn) == 1 then
        table.insert(choices, 1)
      end
      if #player.room.logic:getEventsOfScope(GameEvent.UseCard, 2, function(e)
        local use = e.data
        return use.from == player and use.card.color == Card.Black
      end, Player.HistoryTurn) == 1 then
        table.insert(choices, 2)
      end
      if #choices > 0 then
        event:setCostData(self, choices)
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local prompt = "#sk__qianchong1-choose"
    if #event:getCostData(self) == 1 then
      prompt = "#sk__qianchong"..event:getCostData(self)[1].."-choose"
    end
    local to = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1, prompt, sk__qianchong.name, true)
    if #to > 0 then
      if #event:getCostData(self) == 2 then
        event:setCostData(self, {tos = to, dat = 1, extra = true})
      else
        event:setCostData(self, {tos = to, dat = event:getCostData(self)[1]})
      end
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self).tos[1])
    if event:getCostData(self).dat == 1 then
      local choices = {"sk__qianchong_draw"}
      if to:isWounded() then
        table.insert(choices, 1, "sk__qianchong_recover")
      end
      local choice = room:askForChoice(player, choices, sk__qianchong.name, "#sk__qianchong-choice::"..to.id)
      if choice == "sk__qianchong_recover" then
        room:recover({
          who = to,
          num = math.min(2, to:getLostHp()),
          recoverBy = player,
          skillName = sk__qianchong.name,
        })
      else
        to:drawCards(to.maxHp, sk__qianchong.name)
      end
    else
      local choices = {"sk__qianchong_loseHp"}
      if not to:isNude() then
        table.insert(choices, "sk__qianchong_discard")
      end
      local choice = room:askForChoice(player, choices, sk__qianchong.name, "#sk__qianchong-choice::"..to.id)
      if choice == "sk__qianchong_loseHp" then
        room:loseHp(to, 2, sk__qianchong.name)
      else
        room:askForDiscard(to, to.maxHp, to.maxHp, true, sk__qianchong.name, false)
      end
    end
    local yes = false
    if not player.dead and event:getCostData(self).extra then
      to = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1,
        "#sk__qianchong2-choose", sk__qianchong.name, true)
      if #to > 0 then
        yes = true
        to = room:getPlayerById(to[1])
        local choices = {"sk__qianchong_loseHp"}
        if not to:isNude() then
          table.insert(choices, "sk__qianchong_discard")
        end
        local choice = room:askForChoice(player, choices, sk__qianchong.name, "#sk__qianchong-choice::"..to.id)
        if choice == "sk__qianchong_loseHp" then
          room:loseHp(to, 2, sk__qianchong.name)
        else
          room:askForDiscard(to, to.maxHp, to.maxHp, true, sk__qianchong.name, false)
        end
      end
    end
    if not player.dead and yes and not target:insideExtraTurn() then
      player:gainAnExtraTurn(true, sk__qianchong.name)
    end
  end,
})

return sk__qianchong